package priv.abadstring.study.mbp.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MetaObjectHandlerImpl
 *
 * @author aBadString
 * @date 2021-08-13 15:20:52
 */
@Component
public class MetaObjectHandlerImpl implements MetaObjectHandler {
    /**
     * mp 执行添加操作时执行
     * @param metaObject metaObject.originalObject 是 insert 方法传入的实体对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 通过属性名设置属性值
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("version", 0, metaObject);
    }
    /**
     * mp 执行修改操作时执行
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
